const fs = require('fs');
const path = require('path');
const table = require('text-table');
const { green, magenta, red } = require('chalk');
const { resolve, highlightTheme } = require('hexo-theme-next/scripts/events/lib/utils');

function test(color) {
  const reg = /^(#([\da-fA-F]{6}|[\da-fA-F]{3})|black|white|silver|grey|navy)$/;
  return reg.test(color);
}

const basename = resolve('highlight.js', 'styles/');
const list = ['', 'base16/'].flatMap(relative => {
  return fs.readdirSync(path.join(basename, relative))
    .filter(name => name.endsWith('.css'))
    .map(name => highlightTheme((relative + name).replace('.css', '')))
    .filter(({ background, foreground }) => {
      return !(test(background) && test(foreground));
    });
});

console.log(table(list.map(({ file, background, foreground }) => {
  return [green(file.replace(basename, '')), magenta(background.trim()), red(foreground.trim())];
})));

fs.writeFileSync(path.join(__dirname, '../source/highlight/unavailable.js'), `// This file is automatically generated using \`npm run highlight\`
const unavailable = ${JSON.stringify(list.map(({ file }) => file.replace(basename, '').replace('.css', '')))};
`);
